home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.09 Sep 91 / MW_Source Folder / MWMain < prev   
Encoding:
Text File  |  1990-12-01  |  5.1 KB  |  191 lines  |  [TEXT/PJMM]

  1.  
  2. {        MWMain Unit.}
  3. {    This unit contains the event loop and the loop procedure for the MacTutor demo.}
  4.  
  5. program MWMain;
  6.  
  7.     uses
  8.         MeterWindow;
  9.  
  10. {--------------------------------------------------------------------------------------------}
  11. {        DoMeterWindowDemo procedure}
  12. {    This procedure demonstrates the Meter Window interface.  It uses three nested loops to}
  13. {    create a dummy task that is slow enough to show the Meter Window functionality.  This }
  14. {    procedure accepts one parameter, runSmart.  When runSmart is true, the Meter Window is}
  15. {    displayed during the loop execution; if runSmart is false the then loop is executed but}
  16. {    without creating the Meter Window.}
  17.  
  18.     procedure DoMeterWindowDemo (runSmart: boolean);
  19.  
  20.         const
  21.             outLoop = 5;
  22.             loopsize = 500;
  23.             meterGrade = 20;
  24.  
  25.         var
  26.             i, j, m, n: integer;
  27.             myCursor: CursHandle;
  28.             meterhit, k, innerloop: longint;
  29.             dWorld: SysEnvRec;
  30.             rnum: OSErr;
  31.             vReq: integer;
  32.             iStr: Str255;
  33.  
  34.     begin
  35. {    First determine which processor is in this Mac and then adjust the innerloop parameter}
  36. {    accordingly.  This is done so that the loop will not execute too quickly on high-powered}
  37. {    Macs.}
  38.         vReq := 1;
  39.         rnum := SysEnvirons(vReq, dWorld);
  40.         if rnum = 0 then
  41.             begin
  42.                 if dWorld.processor = env68000 then
  43.                     innerloop := loopsize
  44.                 else if dWorld.processor = env68010 then
  45.                     innerloop := loopsize
  46.                 else if dWorld.processor = env68020 then
  47.                     innerloop := 20 * loopsize
  48.                 else
  49.                     innerloop := 20 * loopsize;
  50.             end
  51.         else
  52.             innerloop := loopsize;
  53. {    Figure out how many loops equal 5% of the total loop.}
  54.         meterHit := longint(round(loopsize / meterGrade));
  55.  
  56.         myCursor := GetCursor(WatchCursor);
  57.         SetCursor(myCursor^^);
  58. {    This is always the first call to the MeterWindow unit.  It initializes and displays }
  59. {    the Window Meter}
  60.         if runSmart then
  61.             mWindowInit;
  62.  
  63.         for n := 1 to outLoop do
  64.             begin
  65. {    for each of the n loops, create a new Meter Window title.}
  66.                 NumToString(n, iStr);
  67.                 iStr := concat('MacTutor Demo - Loop ', iStr);
  68.                 iStr := concat(iStr, ' of 5.');
  69. {    Now eraes the Meter Window and redraw it with a new title.}
  70.                 if runSmart then
  71.                     begin
  72.                         mWindowDraw;
  73.                         mWindowTitle(iStr);
  74.                     end;
  75.                 m := 1;
  76.                 for i := 1 to loopsize do
  77.                     begin
  78. {    When m = meterHit the another 5% of the total loop has been completed and it is time to}
  79. {    update the meter box in the Meter Window.}
  80.                         if m = meterHit then
  81.                             begin
  82.                                 m := 1;
  83.                                 if runSmart then
  84.                                     mWindowUpdate(i, loopsize);
  85.                             end
  86.                         else
  87.                             m := m + 1;
  88.                         k := 0;
  89. {    This is the dummy innerloop that is adjusted based on the current processor.}
  90.                         for j := 1 to innerloop do
  91.                             k := k + 1;
  92.                     end;
  93.             end;
  94. {    All done!  Now destroy the Meter Window.}
  95.         if runSmart then
  96.             mWindowKill;
  97.         InitCursor;
  98.     end;
  99.  
  100. {    This is the main procedure for this demo.  It sets up the Apple and Meter menus and handles}
  101. {    the selection of items from these menus.}
  102.  
  103.     const
  104.         AppleID = 1;
  105.         MeterID = 2;
  106.         AboutItem = 1;
  107.         RunDItem = 1;
  108.         RunSItem = 2;
  109.         QuitItem = 3;
  110.  
  111.     var
  112.         appleMenu, meterMenu: MenuHandle;
  113.         myTitle: string[1];
  114.         daName: Str255;
  115.         dEvent: EventRecord;
  116.         TimeToQuit: boolean;
  117.         dPart, dItem, dMenu, daNum: integer;
  118.         dWindow: WindowPtr;
  119.         dChoice: longint;
  120.  
  121. begin
  122.     InitCursor;
  123. {    Set up the menus.}
  124.     myTitle := ' ';
  125.     myTitle[1] := CHR(appleMark);
  126.     appleMenu := NewMenu(AppleID, myTitle);
  127.     AddResMenu(appleMenu, 'DRVR');
  128.     InsertMenu(appleMenu, 0);
  129.     meterMenu := NewMenu(MeterID, 'Meter');
  130.     AppendMenu(meterMenu, 'Run Dumb');
  131.     AppendMenu(meterMenu, 'Run Smart');
  132.     AppendMenu(meterMenu, 'Quit');
  133.     InsertMenu(meterMenu, 0);
  134.     DrawMenuBar;
  135.  
  136.     TimeToQuit := false;
  137. {    The TimeToQuit variable is set to true when the Quit item is selected from the Meter menu.}
  138.     while not TimeToQuit do
  139.         begin
  140.             SystemTask;
  141.             if GetNextEvent(everyEvent, dEvent) then
  142.                 begin
  143. {    Only handle mousedown events in this simple demo.}
  144.                     case dEvent.what of
  145.                         MouseDown: 
  146.                             begin
  147.                                 dPart := FindWindow(dEvent.where, dWindow);
  148.                                 case dPart of
  149. {    The SysWindow mousedown is for Desk Accessories.}
  150.                                     InSysWindow: 
  151.                                         SystemClick(dEvent, dWindow);
  152.                                     InMenuBar: 
  153.                                         begin
  154. {    Figure out which menu item was selected.}
  155.                                             dChoice := MenuSelect(dEvent.where);
  156.                                             dItem := LoWord(dChoice);
  157.                                             dMenu := HiWord(dChoice);
  158.                                             case dMenu of
  159.                                                 AppleID: 
  160.                                                     begin
  161. {    If an Apple menu item was selected, then do the item.}
  162.                                                         GetItem(appleMenu, dItem, daName);
  163.                                                         daNum := OpenDeskAcc(daName);
  164.                                                     end;
  165.                                                 MeterID: 
  166.                                                     begin
  167.                                                         case dItem of
  168.                                                             RunDItem:
  169. {    The Run Dumb item was selected.  Do the loop processing without displaying the Meter Window.}
  170.                                                                 DoMeterWindowDemo(false);
  171.                                                             RunSItem:
  172. {    Do the loop processing and display the Meter Window.}
  173.                                                                 DoMeterWindowDemo(true);
  174.                                                             QuitItem:
  175. {    Quit and go home.}
  176.                                                                 TimeToQuit := true;
  177.                                                             otherwise
  178.                                                         end;
  179.                                                     end;
  180.                                                 otherwise
  181.                                             end;
  182.                                             HiliteMenu(0);
  183.                                         end;
  184.                                     otherwise
  185.                                 end;
  186.                             end;
  187.                         otherwise
  188.                     end;
  189.                 end;
  190.         end;
  191. end.